package com.ruoyi.framework.web.service;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pay.domain.PayCoinUser;
import com.ruoyi.pay.domain.PayCoinUserForm;
import com.ruoyi.pay.domain.vo.PayCoinUserVO;
import com.ruoyi.pay.service.IPayCoinCashService;
import com.ruoyi.pay.service.IPayCoinUserService;

@Component
public class CoinUserService {

	@Autowired
    private IPayCoinUserService payCoinUserService;
	
	@Autowired
	private IPayCoinCashService payCoinCashService;
	
	public void savePayCoinUser(PayCoinUserForm payCoinUserForm) {
		PayCoinUser coinUser = new PayCoinUser();
		coinUser.setUserId(payCoinUserForm.getUserId());
		coinUser.setCoinNum(payCoinUserForm.getCoinNum());
		coinUser.setCoinType(payCoinUserForm.getCoinType());
		coinUser.setSourceId(payCoinUserForm.getSourceId());
		if(StringUtils.isNotBlank(payCoinUserForm.getIsPay())){
			coinUser.setIsPay(payCoinUserForm.getIsPay());
		}
		coinUser.setRemark(payCoinUserForm.getRemark());
		payCoinUserService.save(coinUser);
	}

	public TableDataInfo getMyCashCoinList(PayCoinUserVO payCoinUser) {
		TableDataInfo data = payCoinUserService.getMyCoinUserList(payCoinUser);
		if("cash".equals(payCoinUser.getPayType())){
			data = payCoinCashService.getMyCoinCashList(payCoinUser);
		}
		return data;
	}

	public BigDecimal getMyCashMoney(PayCoinUserVO payCoinUser) {
		Integer money = payCoinUserService.getMyCashMoney(payCoinUser);
		Integer cash = payCoinCashService.getMyCashMoney(payCoinUser);
		Integer amount = money - cash;
		if(amount < 0){
			amount = 0;
		}
		BigDecimal yue = BigDecimal.valueOf(amount).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP);
		return yue;
	}
}
